package com.example.zjx8969.myapplication.database;

import androidx.room.Dao;
import androidx.room.Insert;
import androidx.room.OnConflictStrategy;
import androidx.room.Query;
import android.text.TextUtils;
import android.util.SparseArray;

import com.example.zjx8969.myapplication.beans.StyleNoListBean;
import com.example.zjx8969.myapplication.beans.SubCategoryListBean;

import java.util.ArrayList;
import java.util.List;

@Dao
public abstract class CategoryDao {
    public static final String CATEGORY_ARTIST = "artist";
    public static final String CATEGORY_BRAND = "brand";
    public static final String CATEGORY_BS = "bs";

    @Insert(onConflict = OnConflictStrategy.REPLACE)
    protected abstract void insertAll(List<SubCategoryListBean> beans);

    @Query("select * from categoryList where category=:category")
    protected abstract List<SubCategoryListBean> getAllByCategory(String category);

    @Query("select * from categoryList ")
    public abstract List<SubCategoryListBean> getAll();

    @Query("delete from categoryList where category=:category")
    abstract void deleteOldData(String category);


    public List<SubCategoryListBean> getByCategory(String category) {
        List<SubCategoryListBean> all = getAllByCategory(category);
        List<StyleNoListBean> allStyle = DataBaseHolder.getInstance().localRelativeDatabase.styleDao().getAll(category);
        SparseArray<List<StyleNoListBean>> group = group(allStyle);
        if (group != null && all != null && !all.isEmpty()) {
            for (SubCategoryListBean bean : all) {
                bean.styleNoList = group.get(bean.id);
            }
        }
        return all;
    }

    private SparseArray<List<StyleNoListBean>> group(List<StyleNoListBean> allStyle) {
        if (allStyle == null || allStyle.isEmpty()) return null;
        SparseArray<List<StyleNoListBean>> listSparseArray = new SparseArray<>(200);

        for (StyleNoListBean bean : allStyle) {
            List<StyleNoListBean> listBeans = listSparseArray.get(bean.parentId);
            if (listBeans == null) {
                listBeans = new ArrayList<>();
                listSparseArray.put(bean.parentId, listBeans);
            }
            listBeans.add(bean);
        }
        return listSparseArray;
    }

    public void insertByCategory(String category, List<SubCategoryListBean> beans) {
        if (TextUtils.isEmpty(category) || beans == null || beans.isEmpty()) return;
        deleteOldData(category);
        insertAll(beans);
        ArrayList<StyleNoListBean> arrayList = new ArrayList<>(100);
        for (SubCategoryListBean bean : beans) {
            bean.category = category;

            if (bean.styleNoList == null || bean.styleNoList.isEmpty())
                continue;
            for (StyleNoListBean styleNoListBean : bean.styleNoList) {
                styleNoListBean.parentId = bean.id;
                styleNoListBean.category = bean.category;
                styleNoListBean.accoutId = bean.accountId;
                arrayList.add(styleNoListBean);
            }
        }
        DataBaseHolder.getInstance().localRelativeDatabase.styleDao().insertAll(arrayList);
    }

}
